layui.use(['table','layer','form'], function(){
	var table = layui.table,
	layer = layui.layer,
	$ = layui.$,
	form = layui.form;
	
	/*@初始化设备分类table*/
	table.render({
	    elem: '#cate-box',
	    id: 'cate',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 0},
	    limit: 'limit',
	    // limits: [5,10,15,20],
	    page: false, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'fault_id', title: 'ID', width: 100},
	    	{field: 'describes', title: '名称', toolbar: '#describes'},
	    	{title: '操作', align:'center', toolbar: '<div><div class="layui-btn" lay-event="edit" onclick="xadmin.open(\'编辑分类\',\'/repair/fault/edit/ty/0/ids/{{d.fault_id}}\')">编辑</div><div class="layui-btn layui-btn-danger del" id="cate" lay-event="del">删除</div></div>'},
	    ]],
	    // done: function(res, curr, count){
	    // 	table.reload('brand',{where:{type: 3,fid: res.data[0].fault_id}})
	    // 	$("tr").eq(1).css("color","red")
	    // },
	});
	/*@初始化设备品牌table*/
	table.render({
	    elem: '#brand-box',
	    id: 'brand',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 3},
	    limit: 5,
	    limits: [5,10,15,20],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '品牌名称'},
	    	{title: '操作', align:'center', toolbar: '<div><div class="layui-btn" lay-event="edit" onclick="xadmin.open(\'编辑品牌\',\'/repair/fault/edit/ty/3/ids/{{d.fault_id}}\')">编辑</div><div class="layui-btn layui-btn-danger del" id="3" lay-event="del">删除</div></div>'},
	    ]]
	});
	/*@初始化设备型号table*/
	table.render({
	    elem: '#marker-box',
	    id: 'marker',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 4},
	    limit: 5,
	    limits: [5,10,15,20],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '型号名称'},
	    	{title: '操作', align:'center', toolbar: '<div><div class="layui-btn" lay-event="edit" onclick="xadmin.open(\'编辑型号\',\'/repair/fault/edit/ty/4/ids/{{d.fault_id}}\')">编辑</div><div class="layui-btn layui-btn-danger del" id="4" lay-event="del">删除</div></div>'},
	    ]]
	});
	/*@初始化故障描述table*/
	table.render({
	    elem: '#fault-box',
	    id: 'fault',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 1},
	    limit: 10,
	    limits: [10,20,25,30,35],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '故障描述'},
	    	{title: '操作', align:'center', toolbar: '<div><div class="layui-btn" lay-event="edit" onclick="xadmin.open(\'编辑故障描述\',\'/repair/fault/edit/ty/1/ids/{{d.fault_id}}\')">编辑</div><div class="layui-btn layui-btn-danger del" id="fault" lay-event="del">删除</div></div>'},
	    ]]
	});
	/*@初始化外观描述table*/
	table.render({
	    elem: '#outward-box',
	    id: 'outward',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 2},
	    limit: 10,
	    limits: [10,20,25,30,35],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '外观描述'},
	    	{title: '操作', align:'center', toolbar: '<div><div class="layui-btn" onclick="xadmin.open(\'编辑外观描述\',\'/repair/fault/edit/ty/2/ids/{{d.fault_id}}\')">编辑</div><div class="layui-btn layui-btn-danger del" id="outward" lay-event="del">删除</div></div>'},
	    ]]
	});
	/*@初始化选择外观描述table*/
	table.render({
	    elem: '#change-outward',
	    id: 'change_outward',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 2},
	    limit: 10,
	    limits: [10,20,25,30,35],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '外观描述'},
	    ]]
	});
	/*@初始化选择故障描述table*/
	table.render({
	    elem: '#change-fault',
	    id: 'change_fault',
	    url: '/repair/fault/index', //数据接口
	    where: {ty: 1},
	    limit: 10,
	    limits: [10,20,25,30,35],
	    page: true, //开启分页
	    cols: [[ //表头
	    	{type: 'checkbox'},
	    	{field: 'describes', title: '故障描述'},
	    ]]
	});
	/*@删除操作*/
	del_db = function(obj, type){
		if(type == 'cate'){
			tit = '确定删除，及其子级';
		}else{
			tit = '确定删除';
		}
		layer.confirm(tit,{icon: 3, title:'提示'},function(index){
	    	$.post("/repair/fault/del/ty/"+type,obj,function(msg){
	    		if(msg.code === 1){
	    			layer.msg(msg.msg,{icon: 6,time: 1000},function(){
	    				table.reload(type)
	    			})
	    		}else{
	    			layer.msg(msg.msg,{icon: 5,time: 1000})
	    		}
	    	})
	    })
	}
  	/*@批量点击删除*/
  	del_all = function (type){
  		var checkStatus = table.checkStatus(type)
	    var data = checkStatus.data;
	    console.log(data)
	    if(data.length == 0){
	    	layer.msg('请选中要删除的数据',{icon: 5})
	    	return
	    }
	    var obj = {}
	    obj.id = []
	    for(var i = 0;i < data.length;++i){
	    	obj.id.push(data[i].fault_id)
	    }
	    del_db(obj, type)
  	}
  	/*@点击保存*/
  	keep_fun = function (type){
  		var checkStatus = table.checkStatus(type)
	    var data = checkStatus.data;
	    if(data.length == 0){
	    	layer.msg('请选中要保存的数据',{icon: 5})
	    	return
	    }
	    var ms = '';
	    for(var i = 0;i < data.length;++i){
	    	ms += ms != '' ? ','+data[i].describes : data[i].describes;
	    }
	    var field = '';
	    switch(type){
	    	case 'change_fault': field = 'gz_describe';break;
	    	case 'change_outward': field = 'wg_describe';break;
	    }
	    window.parent.$("input[name='"+field+"']").val(ms);
    	xadmin.close();
  	}
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'zcc-show-btn'){ //展开、关闭
	  		var len = $(tr).find('.zcc-hide-icon').length
	    	hide_show(data.fault_id, len)
	    	if(len == 0){
	    		$(tr).find('.zcc-icon').addClass('zcc-hide-icon')
	    	}else{
	    		$(tr).find('.zcc-icon').removeClass('zcc-hide-icon')
	    	}
	  	} else if(layEvent === 'del'){ //删除
	  		var obj = {}
	  		obj.id = data.fault_id
	  		var type = $(tr).find('.del').attr('id')
	    	del_db(obj, type)
	  	}else if(layEvent === 'edit'){ //
	    	//do something
	    	console.log(111)
	    	//同步更新缓存对应的值
	    	obj.update({
	      		repair_sn: '123',
	      		title: 'xxx'
	    	});
	  	}
	});
	/*@显示、关闭*/
	hide_show = function (fid, len){
		if(len == 0){
			$('.fid_'+fid).parents('tr').hide()
		}else{
			$('.fid_'+fid).parents('tr').show()
			$('.fid_'+fid).find('.zcc-icon').removeClass('zcc-hide-icon')
		}
		// $('.fid_'+fid).parents('tr').toggle()
		$('.fid_'+fid).each(function(index){
			var fault_id = $(this).attr('fault_id')
			if($('.fid_'+fault_id).length > 0){
				hide_show(fault_id, len)
			}
		})
	}
})